/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Truck.java
 *
 * Created on Oct 29, 2011, 3:44:20 PM
 */
package VIMS;
import java.sql.*;
import javax.swing.JOptionPane;


/**
 *
 * @author shyan
 */
public class Truck extends javax.swing.JFrame {

    private Database dbOB = new Database();
    private ResultSet rset = null;
    private String action ="";
    private String selectedID=null;
    
    /** Creates new form Truck */
    public Truck() {
        super("Add New Truck");
        initComponents();
        setLocationRelativeTo(null);
        DateLabel.setVisible(false);

    }
    
    public Truck(String choice,String selectedRowID) {
        super(choice+" Truck");
        initComponents();
        setLocationRelativeTo(null);  
        DateLabel.setVisible(false);
        action=choice;
        selectedID = selectedRowID;
        
        if(action.equals("Add"))
        {
            TruckButton.setText(action);
            NextTruckID();
            //FormattedField();
        }
        if(action.equals("Delete"))
        {
            TruckButton.setText(action);
            ComponentDISABLED();
            DisplayTruckInfo();
            
        }
    }
    
    
    //Retrive Next New Truck Number
    private void NextTruckID() 
    { 
        int currentNewEnum=0;
        Database ob = new Database();
        ResultSet rset = ob.getResultSet("SELECT MAX(T_NUMBER) FROM truck");
        try{
            if(rset.next())
                currentNewEnum=rset.getInt(1)+1;
           }
        catch(SQLException ex)
           {
             ex.printStackTrace();
           }
        ID.setText(Integer.toString(currentNewEnum));
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        ID = new javax.swing.JLabel();
        UidLabel = new javax.swing.JLabel();
        PlatNumber = new javax.swing.JTextField();
        UnLabel = new javax.swing.JLabel();
        FnLabel = new javax.swing.JLabel();
        Manufacturer = new javax.swing.JTextField();
        TModelLabel = new javax.swing.JLabel();
        Model = new javax.swing.JTextField();
        TPurchaseYearLabel = new javax.swing.JLabel();
        Capacity = new javax.swing.JTextField();
        TCapacityLabel = new javax.swing.JLabel();
        TStatusLabel = new javax.swing.JLabel();
        Status = new javax.swing.JComboBox();
        TruckButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        Photo = new javax.swing.JLabel();
        Year = new javax.swing.JComboBox();
        Month = new javax.swing.JComboBox();
        Day = new javax.swing.JComboBox();
        note = new javax.swing.JLabel();
        DateLabel = new javax.swing.JLabel();
        Border = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));

        jLayeredPane1.setMaximumSize(new java.awt.Dimension(800, 600));
        jLayeredPane1.setMinimumSize(new java.awt.Dimension(800, 600));

        ID.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        ID.setBounds(490, 90, 150, 30);
        jLayeredPane1.add(ID, javax.swing.JLayeredPane.DEFAULT_LAYER);

        UidLabel.setText("Truck ID   :");
        UidLabel.setBounds(390, 90, 90, 30);
        jLayeredPane1.add(UidLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        PlatNumber.setBounds(490, 130, 150, 30);
        jLayeredPane1.add(PlatNumber, javax.swing.JLayeredPane.DEFAULT_LAYER);

        UnLabel.setText("Plat Number   :");
        UnLabel.setBounds(390, 130, 90, 30);
        jLayeredPane1.add(UnLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FnLabel.setText("Manufacturer   :");
        FnLabel.setBounds(390, 170, 90, 30);
        jLayeredPane1.add(FnLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        Manufacturer.setBounds(490, 170, 150, 30);
        jLayeredPane1.add(Manufacturer, javax.swing.JLayeredPane.DEFAULT_LAYER);

        TModelLabel.setText("Model   :");
        TModelLabel.setBounds(390, 210, 90, 30);
        jLayeredPane1.add(TModelLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        Model.setBounds(490, 210, 150, 30);
        jLayeredPane1.add(Model, javax.swing.JLayeredPane.DEFAULT_LAYER);

        TPurchaseYearLabel.setText("Purchase Date   :");
        TPurchaseYearLabel.setBounds(390, 290, 100, 30);
        jLayeredPane1.add(TPurchaseYearLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        Capacity.setBounds(490, 250, 150, 30);
        jLayeredPane1.add(Capacity, javax.swing.JLayeredPane.DEFAULT_LAYER);

        TCapacityLabel.setText("Capacity   :");
        TCapacityLabel.setBounds(390, 250, 90, 30);
        jLayeredPane1.add(TCapacityLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        TStatusLabel.setText("Status   :");
        TStatusLabel.setBounds(390, 330, 100, 30);
        jLayeredPane1.add(TStatusLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Status.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose", "AVAILABLE", "UNAVAILABLE" }));
        Status.setBounds(490, 330, 120, 30);
        jLayeredPane1.add(Status, javax.swing.JLayeredPane.DEFAULT_LAYER);

        TruckButton.setText("Add");
        TruckButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TruckButtonActionPerformed(evt);
            }
        });
        TruckButton.setBounds(580, 550, 80, 30);
        jLayeredPane1.add(TruckButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });
        CancelButton.setBounds(660, 550, 80, 30);
        jLayeredPane1.add(CancelButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Photo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Photo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Photos/audi_R8_TDI_LeMans_RedManace.jpg"))); // NOI18N
        Photo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        Photo.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        Photo.setBounds(30, 50, 320, 470);
        jLayeredPane1.add(Photo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Year.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Year", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));
        Year.setBounds(490, 290, 80, 30);
        jLayeredPane1.add(Year, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Month.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Month", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));
        Month.setBounds(580, 290, 80, 30);
        jLayeredPane1.add(Month, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Day.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Day", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        Day.setBounds(670, 290, 80, 30);
        jLayeredPane1.add(Day, javax.swing.JLayeredPane.DEFAULT_LAYER);

        note.setFont(new java.awt.Font("Tahoma", 2, 11));
        note.setForeground(new java.awt.Color(255, 0, 51));
        note.setText("*plalet");
        note.setBounds(650, 250, 80, 30);
        jLayeredPane1.add(note, javax.swing.JLayeredPane.DEFAULT_LAYER);
        DateLabel.setBounds(490, 290, 150, 30);
        jLayeredPane1.add(DateLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Border.setFont(new java.awt.Font("Tahoma", 1, 18));
        Border.setBorder(javax.swing.BorderFactory.createTitledBorder("Truck's Details :"));
        Border.setBounds(370, 50, 400, 350);
        jLayeredPane1.add(Border, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void TruckButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TruckButtonActionPerformed
        if(TruckButton.getText().equals("Delete"))
        {
            Status.setSelectedItem("UNAVAILABLE");
            DeleteTruck();
        }
        else 
            AddTruck();  
    }//GEN-LAST:event_TruckButtonActionPerformed

    
    private void AddTruck()
    {
        String T_ID         =ID.getText();
        String T_PlatNum    =PlatNumber.getText().trim(); 
        String T_ManuFac    =Manufacturer.getText().trim();
        String T_Model      =Model.getText().trim();
        String T_Space      =Capacity.getText().trim();
        String BuyDate      =Year.getSelectedItem().toString()+"-"+Month.getSelectedItem().toString()+"-"+Day.getSelectedItem().toString();
        String T_Status     =Status.getSelectedItem().toString();
        
       
        if(T_ID.isEmpty()|| T_PlatNum.isEmpty() || T_ManuFac.isEmpty() || T_Model.isEmpty() || T_Space.isEmpty() || Year.getSelectedItem().equals("Year")
                         || Month.getSelectedItem().equals("Month")    || Day.getSelectedItem().equals("Day") || T_Status.equals("Choose") )            
        {
            JOptionPane.showMessageDialog(null, "Please fill up all information.");
        }
        else
        {
            String message = "Add a new truck?";
            String title = "Confirmation?";
            int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (reply == JOptionPane.YES_OPTION)
            {
                String SQL="INSERT INTO truck VALUES ('"+T_ID+"','"+T_PlatNum+"','"+T_ManuFac+"','"+T_Model+"','"+BuyDate+"','"+T_Space+"','"+T_Status+"')";

                if(dbOB.update(SQL)==true)
                {
                     JOptionPane.showMessageDialog(null,"New truck is added.");
                     this.setVisible(false);
                }
                else
                      JOptionPane.showMessageDialog(null, "Data failed to be added into database.\nPlease check the entered information!");                 
            }
        }
    }
    
    private void DeleteTruck()
    {
        String title = "Confirmation";
        String msg   = "Deactivate truck "+ID.getText()+" ?";
        int reply = JOptionPane.showConfirmDialog(null, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if(reply==JOptionPane.YES_OPTION)
        {
            String SQL="UPDATE truck SET T_STATUS='UNAVAILABLE' WHERE T_NUMBER="+selectedID;
            if(dbOB.update(SQL)==true)
            {
                JOptionPane.showMessageDialog(null , "Truck "+ID.getText()+" has been Deactivated.");
                this.setVisible(false);
            }
            else
                JOptionPane.showMessageDialog(null , "Failure occurs during deletion.");
        }
        else
            Status.setSelectedItem("AVAILABLE");
    }
    
    
    // Retrive Selected Truck Details
    void DisplayTruckInfo()
    {
        String SQL= "SELECT * FROM truck WHERE T_NUMBER ="+selectedID;
        rset = dbOB.getResultSet(SQL);
        try
        {
          if(rset.next())
          {
                ID.setText(rset.getString(1));
                PlatNumber.setText(rset.getString(2));
                Manufacturer.setText(rset.getString(3));
                Model.setText(rset.getString(4));
                DateLabel.setText(rset.getString(5));
                Capacity.setText(rset.getString(6));
                Status.setSelectedItem(rset.getString(7));
          }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }  
    }
    
    
    private void ComponentDISABLED()
    {
        PlatNumber.setEditable(false);
        Manufacturer.setEditable(false);
        Model.setEditable(false);
        Capacity.setEditable(false);
        Year.setVisible(false);
        Month.setVisible(false);
        Day.setVisible(false);
        Status.setEnabled(false);
        
        DateLabel.setVisible(true);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Truck.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Truck.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Truck.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Truck.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Truck().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Border;
    private javax.swing.JButton CancelButton;
    private javax.swing.JTextField Capacity;
    private javax.swing.JLabel DateLabel;
    private javax.swing.JComboBox Day;
    private javax.swing.JLabel FnLabel;
    private javax.swing.JLabel ID;
    private javax.swing.JTextField Manufacturer;
    private javax.swing.JTextField Model;
    private javax.swing.JComboBox Month;
    private javax.swing.JLabel Photo;
    private javax.swing.JTextField PlatNumber;
    private javax.swing.JComboBox Status;
    private javax.swing.JLabel TCapacityLabel;
    private javax.swing.JLabel TModelLabel;
    private javax.swing.JLabel TPurchaseYearLabel;
    private javax.swing.JLabel TStatusLabel;
    private javax.swing.JButton TruckButton;
    private javax.swing.JLabel UidLabel;
    private javax.swing.JLabel UnLabel;
    private javax.swing.JComboBox Year;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLabel note;
    // End of variables declaration//GEN-END:variables
}
